#import "SEGWinningrecordlistManager.h"

#if __has_include(<Commom/SEGModel.h>)
#import <Commom/SEGModel.h>
#else
#import "SEGModel.h"
#endif
#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

static id _winningRecordListManager;
static dispatch_once_t onceToken;

@implementation SEGWinningrecordlistManager
#pragma mark - SINGLE----BEGIN----
+(instancetype)sharedManager{
    dispatch_once(&onceToken, ^{
        _winningRecordListManager = [[self alloc] init];
    });
    return _winningRecordListManager;
}

+(void)destroyResource{
    onceToken = 0;
    _winningRecordListManager = nil;
}

#pragma mark - SINGLE----END----

#pragma mark - HTTP----BEGIN----

/// 中奖纪录列表接口
-(void)requestWinningrecordlistWithPageno:(NSString*)pageNo pageLimit:(NSString*)pageLimit finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:pageNo forKey:@"pageNo"];
    [params setValue:pageLimit forKey:@"pageLimit"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/example/winningRecordList",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed) {
            finished(isSuccessed,nil,codeType,error.localizedDescription);
            return;
        }
        
        SEGWinningrecordlistModel *lastestData = [SEGWinningrecordlistModel yy_modelWithDictionary:result];
        
        if (pageNo.integerValue != 1 && lastestData.data.winningRecordList.count == 0) {
            finished(NO, result, codeType, @"没有更多的数据！");
            return;
        }
        
        if (pageNo.integerValue == 1) { //顶部刷新
            self.winningRecordList = [SEGWinningrecordlistModel yy_modelWithDictionary:result];
        }else {
            NSMutableArray* _tmpData = [NSMutableArray arrayWithArray:self.winningRecordList.data.winningRecordList];
            [_tmpData addObjectsFromArray:lastestData.data.winningRecordList];
            self.winningRecordList.data.winningRecordList = [NSArray arrayWithArray:_tmpData];
        }
        
        finished(isSuccessed,result,codeType,error.localizedDescription);
        return;
    }];
}
#pragma mark - HTTP----END----

#pragma mark - GETTER----BEGIN----
-(SEGWinningrecordlistModel *)winningRecordList{
    if (!_winningRecordList) {
        _winningRecordList = [[SEGWinningrecordlistModel alloc]init];
    }
    return _winningRecordList;
}
#pragma mark - GETTER----END----
@end

